package mshop.com.schild_kotlin.mvp.presenter

import android.content.Context
import io.reactivex.Observable
import io.reactivex.functions.Consumer
import mshop.com.schild_kotlin.base.applySchedulers
import mshop.com.schild_kotlin.entitys.HomeBean
import mshop.com.schild_kotlin.mvp.contract.HomeContract
import mshop.com.schild_kotlin.mvp.model.HomeModel
import mshop.com.schild_kotlin.network.MyObserver

/**
 * Created by mayi on 2018/5/24.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO
 */
class HomePresenter(context: Context?, view: HomeContract.View) : HomeContract.Presenter {
    var mContext: Context? = null
    var mView: HomeContract.View? = null
    val mModel: HomeModel by lazy {
        HomeModel()
    }

    init {
        mContext = context
        mView = view
    }

    override fun start() {
        requestData()
    }

    override fun requestData() {

//        observable?.applySchedulers()?.subscribe { homeBean : HomeBean ->
//            mView?.setData(homeBean)
//        }

        mContext?.let {
            val observable: Observable<HomeBean>? = mModel.loadData(it, true, "0")
            observable?.applySchedulers()?.subscribe(object : MyObserver<HomeBean>(it) {
                override fun onNext(t: HomeBean) {
                    mView?.setData(t)
                }
            })
        }

//        mContext?.let {
//            val observable: Observable<HomeBean>? = mModel.loadData(it, true, "0")
//            observable?.applySchedulers()?.subscribe(object :Observer<HomeBean>{
//                override fun onComplete() {
//
//                }
//
//                override fun onSubscribe(d: Disposable?) {
//                }
//
//                override fun onNext(t: HomeBean?) {
//                    if (t != null) {
//                        mView?.setData(t)
//                    }
//                }
//
//                override fun onError(e: Throwable?) {
//                }
//
//            })
//        }
    }

    open fun moreData(data: String) {
//        val observable: Observable<HomeBean>? = mContext?.let { mModel.loadData(it, false, data) }
//        observable?.applySchedulers()?.subscribe(Consumer { mView?.setData(it) })


        mContext?.let {
            val observable: Observable<HomeBean>? = mModel.loadData(it, false, data)
            observable?.applySchedulers()?.subscribe(object : MyObserver<HomeBean>(it) {
                override fun onNext(t: HomeBean) {
                    mView?.setData(t)
                }
            })
        }
    }

}